# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import HttpResponse
from django.core.serializers.json import DjangoJSONEncoder

import json
import decimal


# Create your views here.

def decimal_default(obj):
    if isinstance(obj, decimal.Decimal):
        return float(obj)
    raise TypeError


def response(is_success = True, data = None, success = '', error = ''):
    if is_success:
        return success_response(data, success)
    else:
        return error_response(error)


def success_response(data = {}, message = ''):
    result = {}
    result['errcode'] = 0
    result['message'] = message
    if data: result['data'] = data
    # return HttpResponse(json.dumps(result, default=decimal_default), content_type='application/json')
    return HttpResponse(json.dumps(result, cls=DjangoJSONEncoder), content_type='application/json')


def error_response(message = ''):
    result = {}
    result['errcode'] = 1
    result['message'] = message
    # return HttpResponse(json.dumps(result, default=decimal_default), content_type='application/json')
    return HttpResponse(json.dumps(result, cls=DjangoJSONEncoder), content_type='application/json')

# --------------------------------------------------------------------------------------------------

if __name__ == '__main__':
    print('ok')